package com.eco.fanliapp.job.goodsjob;


import com.eco.fanliapp.entity.SyChannelGoods;
import com.eco.fanliapp.service.ISyChannelGoodsService;
import com.eco.fanliapp.service.RedisTemplateService;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.util.List;

/**
 * 高佣商品，佣金和固定返不对称渠道下商品
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class SyChannelGoodsTask implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(SyChannelGoodsTask.class);

    @Autowired
    private ISyChannelGoodsService syChannelGoodsService;
    @Autowired
    private RedisTemplateService redisTemplateService;


    public SyChannelGoodsTask() {
    }

    @Override
    public void execute(JobExecutionContext context) {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************开始执行高佣商品，佣金和固定返不对称渠道下商品定时扫描任务-GoodsFreeTask：executeCount=" + executeCount + "--startTime=" + startTime);

        //当前时间
        long startMillions = System.currentTimeMillis();

        try {
            //匹配所有有效商品，对比高佣固定返是否对称
            List<SyChannelGoods> list = syChannelGoodsService.findChannelGoodsCommission();
            if(CollectionUtils.isNotEmpty(list)){
                //删除高佣商品缓存
                String redisKeyStr = "app_channel_good_List";
                redisTemplateService.del(redisKeyStr);

                //批量删除渠道下商品
                syChannelGoodsService.updateBatchById(list);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行免单过期商品定时扫描任务错误");
        }

        long endTime = System.currentTimeMillis();
        log.info("***************************************结束执行高佣商品，佣金和固定返不对称渠道下商品定时扫描任务-GoodsFreeTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));

    }

}
